﻿/** 客户信息补录 **/
CREATE PROCEDURE [dbo].[proc_Customer_Additional]
(
	 @CusId	uniqueidentifier
	,@CusPerson	nvarchar(50)
	,@CusRegTime datetime
	,@CusRegCapital decimal(18,2)
	,@CusEmployees nvarchar(50)
	,@CusIntroduction nvarchar(max)
	,@CusPersonAge int
	,@CusPersonWeixin nvarchar(50)
)
AS
Begin
	Begin Tran
	if(@CusEmployees<>'NULL')
		Update Customer Set 
			RegTime=@CusRegTime,
			RegCapital=@CusRegCapital,
			Employees=@CusEmployees,
			Introduction=@CusIntroduction
		Where CustomerId=@CusId
	else
		Update Customer Set 
			RegTime=@CusRegTime,
			RegCapital=@CusRegCapital,
			Introduction=@CusIntroduction
		Where CustomerId=@CusId

	Declare @cpId int;
	Select Top 1 @cpId=Id From ContactPerson 
		Where CustomerId=@CusId And 
		((Mobile<>'' And (Mobile Is Not Null)) Or 
		(OfficeTel<>'' And (OfficeTel Is Not Null)) Or 
		(HomeTel<>'' And (HomeTel Is Not Null)))
		Order By IsMain Desc,PersonTypeId,CreateDate Desc
	--更新联系人年龄、微信
	Update ContactPerson Set
		Age=@CusPersonAge,
		Weixin=@CusPersonWeixin,
		ZName=@CusPerson

	where Id=@cpId

	If @@error <> 0  --发生错误
		Begin
			RollBack Tran
			Return 3
		End
	Else
		Begin
			Commit Tran
			Return 1	--执行成功
		End
End
